package com.xdk.templatescala.common.response;

import java.util.Optional;

/**
 * @author jhz
 * @description Result 接口统一返回数据格式
 * @date 2021/6/1
 **/
public class Result <T>{

    private int code;

    private String msg;

    private Long totalSize;

    private T data;

    public int getCode() {
        return code;
    }

    public Result<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Long getTotalSize() {
        return totalSize;
    }

    public Result<T> setTotalSize(Long totalSize) {
        this.totalSize = totalSize;
        return this;
    }

    /**
     * 操作成功，有结果返回
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result SUCCESS(T data) {
        return buildRespMsg(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    /**
     * 操作成功，有结果返回
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result SUCCESS(T data, long totalSize) {
        return buildRespMsg(ResultCode.SUCCESS.getCode(), totalSize, ResultCode.SUCCESS.getMsg(), data);
    }

    /**
     * 操作成功，无结果返回
     *
     * @param <T>
     * @return
     */
    public static <T> Result SUCCESS() {
        return buildRespMsg(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
    }

    /**
     * 操作失败，有结果返回
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result FAIL(T data) {
        return buildRespMsg(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMsg(), data);
    }

    /**
     * 操作失败，有结果返回
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result FAIL(ResultCode code,T data) {
        return buildRespMsg(code.getCode(), code.getMsg(), data);
    }



    /**
     * 操作失败，有结果返回
     *
     * @param <T>
     * @return
     */
    public static <T> Result PARAM_NOT_NULL() {
        return buildRespMsg(ResultCode.PARAM_NOT_NULL.getCode(), ResultCode.PARAM_NOT_NULL.getMsg(), null);
    }

    /**
     * 操作失败
     *
     * @param <T>
     * @return
     */
    public static <T> Result SYSTEM_ERROR() {
        return buildRespMsg(ResultCode.SYSTEM_ERROR.getCode(), ResultCode.SYSTEM_ERROR.getMsg(), null);
    }


    private static <T> Result buildRespMsg(int code, String msg, T data) {
        Result respMsg = new Result();
        respMsg.setCode(code);
        respMsg.setMsg(msg);
        if (Optional.ofNullable(data).isPresent()) {
            respMsg.setData(data);
        }
        return respMsg;
    }

    private static <T> Result buildRespMsg(int code, long totalSize, String msg, T data) {
        Result respMsg = new Result();
        respMsg.setCode(code);
        respMsg.setMsg(msg);
        respMsg.setTotalSize(totalSize);
        if (Optional.ofNullable(data).isPresent()) {
            respMsg.setData(data);
        }
        return respMsg;
    }
}
